package com.hqhsc.core.interceptors;

import java.util.Properties;

import com.hqhsc.module.login.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.hqhsc.module.common.service.ManageMemcacdedClient;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MethodTimeConsumingInterceptor extends HandlerInterceptorAdapter {
	final Logger logger = LoggerFactory.getLogger(MethodTimeConsumingInterceptor.class);
	private long preTime = 0l;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		logger.debug("执行资源调用耗时拦截器的preHandle方法,该方法在控制层的方法被调用前调用!");
		User user = (User) request.getSession().getAttribute("user");//登陆用户的User对象

		String str = request.getRequestURI();
		if (user == null) {
            if(str != null && str.contains(request.getContextPath() + "/manage/")) {
                logger.info("用户的Session对象中不包含User对象,系统判定Session失效,跳转到网站首页");
                response.sendRedirect(request.getContextPath() + "/login");
                return false;
            }
		}

		preTime = System.currentTimeMillis();
		request.setAttribute("preTime", preTime);
		return true;
	}
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		long postTime = System.currentTimeMillis();
		long temp = postTime - preTime;
		logger.debug("执行资源调用耗时拦截器的postHandle方法,该方法在控制层的方法被调用后调用!,耗时{}毫秒", temp);
		request.setAttribute("postTime", postTime);
		super.postHandle(request, response, handler, modelAndView);
	}
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		/*
		 * 由于这个方法是在渲染视图后运行，�?��在该方法中设置request属�?没有效果
		 */
		long completionTime = System.currentTimeMillis();
		long temp = completionTime - preTime;
		logger.debug("执行资源调用耗时拦截器的afterCompletion方法,该方法在视图被渲染后被调用,耗时{}毫秒", temp);
		super.afterCompletion(request, response, handler, ex);
	}
}
